//
//  BidHeaderView.swift
//  cxd4iphone
//
//  Created by hexy on 12/2/15.
//  Copyright © 2015 hexy. All rights reserved.
//

import UIKit
//private struct kConstraints {
//    
//    static let cornerRadius: CGFloat = 8
//    static let wrapEdges: UIEdgeInsets = UIEdgeInsetsMake(25, 40, -20, -40)
//    
//    static let progressHeight: CGFloat = xx_height(12)
//}
private struct kConstraints {
    
    static let wrapEdges: UIEdgeInsets = UIEdgeInsetsMake(xx_height(10), 0, -xx_height(10), 0)
    static let ratewrapEdges: UIEdgeInsets = UIEdgeInsetsMake(xx_height(10), 60, rateViewMarginBottom - xx_height(10), -60)
    
    static let lineColor: UIColor = xx_colorWithHex(hexValue: 0xededed)
    static let bgColor: UIColor = xx_colorWithHex(hexValue: 0xf8f8f8)
    static let xRedColor: UIColor = xx_colorWithHex(hexValue: 0xf64242)
    static let xBlueColor: UIColor = xx_colorWithHex(hexValue: 0x3d64b4)
    static let lightFontColor: UIColor = xx_colorWithHex(hexValue: 0x666666)
    static let darkFontColor: UIColor = xx_colorWithHex(hexValue: 0x333333)

    static let bottomViewHeight: CGFloat = xx_height(70)
    static let divideViewHeight: CGFloat = xx_height(10)
    static let timeViewHeight: CGFloat = xx_height(30)
    static let timeViewMarginBottom: CGFloat = -(bottomViewHeight + divideViewHeight)
    static let rateViewMarginBottom: CGFloat = -(bottomViewHeight + divideViewHeight + timeViewHeight + 1)
    
    
}

class BidInfoTopView: UIView {

    
    var bidViewModel: BidViewModel? {
        
        didSet {
            
            let vm = bidViewModel!
            
            lastTimeBtn.setTitle(vm.investLastTime, forState: .Normal)
            releaseTimeBtn.setTitle(vm.releaseTime, forState: .Normal)
            progressView.bidViewModel = vm
            rateNumLabel.text = vm.bidModel.rate!
        }
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        makeUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func makeUI() {
        
        backgroundColor = Define.backgroundColor
        
        addSubview(wrap)
        wrap.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(kConstraints.wrapEdges)
        }
        
        makeBottomView()
        
        addSubview(rateWrap)
        rateWrap.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(kConstraints.ratewrapEdges)
        }
        makeTimeView()
        
        rateWrap.addSubview(progressView)
//        progressView.backgroundColor = UIColor.lightGrayColor()
        progressView.snp_makeConstraints { (make) -> Void in
            
            make.left.right.equalTo(rateWrap)
            make.bottom.equalTo(rateWrap).offset(xx_height(-10))
        }
        
        makeRateView()
    }
    
    lazy var pDetaiBtn : UIButton = {
        
        return TBButton(title: "项目详情", image: UIImage(named: "buy_detail")!)
    }()
    
    lazy var pRiskBtn : UIButton = {
        
        return TBButton(title: "项目风控", image: UIImage(named: "buy_risk")!)
    }()
    
    lazy var bidHistoryBtn : UIButton = {
        
        return TBButton(title: "投资记录", image: UIImage(named: "buy_history")!)
    }()
    
    lazy var lastTimeBtn = UIButton(title: "8天封闭期", titleColor: Define.moreLightFontColor, titleSize: 12, bgColor: UIColor.whiteColor(), normalImage: UIImage(named: "buy_last"))
    
    lazy var  releaseTimeBtn = UIButton(title: "发布时间:2015-11-12", titleColor: Define.moreLightFontColor, titleSize: 12, bgColor: UIColor.whiteColor(), normalImage: UIImage(named: "buy_begin"))
    
    lazy var progressView = ProgressView(height: xx_height(8))
    
    lazy var wrap: UIView = {
        
        let rateWrap = UIView(bgColor: UIColor.whiteColor())
//        rateWrap.backgroundColor = xx_randomColor()
        return rateWrap
    }()
    
    lazy var rateWrap: UIView = {
        
        let rateWrap = UIView()
//                rateWrap.backgroundColor = xx_randomColor()
        return rateWrap
    }()
    
    lazy var rateNumLabel:UILabel = {
        
        var fontSize: CGFloat = 0
        
        if xx_iPhone4() {
            
            fontSize = 60
        }
        if xx_iPhone5() {
            
            fontSize = 80
        }
        
        if xx_iPhone6() {
            
            fontSize = 90
        }
        
        if xx_iPhone6p() {
            
            fontSize = 90
            
        }
        return UILabel(num: "20", fontSize: fontSize, fontColor: Define.redColor)
        
    }()
    lazy var percentLabel:UILabel = {
        
        var fontSize: CGFloat = 0
        
        if xx_iPhone4() {
            
            fontSize = 23
        }
        if xx_iPhone5() {
            
            fontSize = 30
        }
        if xx_iPhone6() {
            
            fontSize = 40
        }
        if xx_iPhone6p() {
            
            fontSize = 40
        }
        return UILabel(num: "%", fontSize: fontSize, fontColor: Define.redColor)
    }()

}


extension BidInfoTopView {
    
    func makeRateView() {
        
        
        
        let view = UIView(bgColor: UIColor.lightGrayColor())
        
        rateWrap.addSubview(view)
        view.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(rateWrap).offset(xx_height(-8))
            make.centerX.equalTo(rateWrap)
        }
        
        view.addSubview(rateNumLabel)
//                rateNumLabel.backgroundColor = xx_randomColor()
        rateNumLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.left.equalTo(view)
            
        }
        
//                percentLabel.backgroundColor = xx_randomColor()
        view.addSubview(percentLabel)
        percentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(rateNumLabel.snp_right)
            make.right.equalTo(view)
            
            let centerYoffset = xx_iPhone4() ? xx_folat(12) : xx_folat(18)
            
            make.centerY.equalTo(rateNumLabel).offset(centerYoffset)
        }
        
        let yearRateLabel = UILabel(title: "年化收益率", fontSize: 12)
        
        rateWrap.addSubview(yearRateLabel)
        yearRateLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(percentLabel.snp_bottom)//.offset(xx_height(-10))
            make.centerX.equalTo(rateWrap)
        }

    }
    
    func makeTimeView() {
        
        let middleView = UIView()

        wrap.addSubview(middleView)
        middleView.snp_makeConstraints { (make) -> Void in

            make.bottom.equalTo(wrap).offset(kConstraints.timeViewMarginBottom)
            make.left.equalTo(wrap).offset(15)
            make.right.equalTo(wrap).offset(-15)
            make.height.equalTo(xx_height(30))
        }

        let hLine = UIView(bgColor: kConstraints.lineColor)

        middleView.addSubview(hLine)
        hLine.snp_makeConstraints { (make) -> Void in

            make.top.left.right.equalTo(middleView)
            make.height.equalTo(1)
        }

        let vLine = UIView(bgColor: kConstraints.lineColor)

        middleView.addSubview(vLine)
        vLine.snp_makeConstraints { (make) -> Void in

            make.center.equalTo(middleView)
            make.height.equalTo(xx_height(15))
            make.width.equalTo(1)
        }


        middleView.addSubview(lastTimeBtn)
        lastTimeBtn.snp_makeConstraints { (make) -> Void in

            make.left.bottom.equalTo(middleView)
            make.right.equalTo(vLine.snp_left)
            make.top.equalTo(hLine.snp_bottom)
        }

        
        middleView.addSubview(releaseTimeBtn)
        releaseTimeBtn.snp_makeConstraints { (make) -> Void in
            
            make.right.bottom.right.equalTo(middleView)
            make.left.equalTo(vLine.snp_right)
            make.top.equalTo(hLine.snp_bottom)
        }

        
    }
    
    func makeBottomView() {
        
        let bottomView = UIView()
        bottomView.backgroundColor = xx_randomColor()
        wrap.addSubview(bottomView)
        bottomView.snp_makeConstraints { (make) -> Void in
            
            make.left.bottom.right.equalTo(wrap)
            make.height.equalTo(kConstraints.bottomViewHeight)
        }
        
        let divideView = UIView(bgColor: kConstraints.bgColor)
        
        wrap.addSubview(divideView)
        divideView.snp_makeConstraints { (make) -> Void in
            
            make.left.right.equalTo(wrap)
            make.bottom.equalTo(bottomView.snp_top)
            make.height.equalTo(kConstraints.divideViewHeight)
        }
        
        
        bottomView.addSubview(pDetaiBtn)
        pDetaiBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.left.bottom.equalTo(bottomView)
        }
        
        bottomView.addSubview(pRiskBtn)
        pRiskBtn.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(pDetaiBtn.snp_right)
            make.top.bottom.equalTo(bottomView)
            make.width.equalTo(pDetaiBtn)
        }
        
        bottomView.addSubview(bidHistoryBtn)
        bidHistoryBtn.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(pRiskBtn.snp_right)
            make.width.equalTo(pDetaiBtn)
            make.top.bottom.right.equalTo(bottomView)
        }
        
        let vLine1 = UIView(bgColor: kConstraints.lineColor)
        
        bottomView.addSubview(vLine1)
        vLine1.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(pDetaiBtn.snp_right)
            make.centerY.equalTo(bottomView)
            make.height.equalTo(xx_height(35))
            make.width.equalTo(1)
        }
        
        let vLine2 = UIView(bgColor: kConstraints.lineColor)
        
        bottomView.addSubview(vLine2)
        vLine2.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(pRiskBtn.snp_right)
            make.centerY.equalTo(bottomView)
            make.height.equalTo(xx_height(35))
            make.width.equalTo(1)
        }

    }
}
